home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / By the Book / Mac C Primer V1 CW9 / 68K and PPC Projects (CW9) / 7.02 - Pager / Pager.c < prev    next >
C/C++ Source or Header  |  1996-06-01  |  6KB  |  297 lines

  1. /********************************************************/
  2. /*                                                        */
  3. /*  Pager Code from Chapter Seven of                    */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11. #include <limits.h>
  12.  
  13. #define kBaseResID            128
  14. #define kMoveToFront        (WindowPtr)-1L
  15. #define kScrollBarWidth        16
  16. #define    kNilActionProc        nil
  17. #define kSleep                LONG_MAX
  18.  
  19. #define    kVisible            true
  20. #define    kStartValue            1
  21. #define    kMinValue            1
  22. #define    kNilRefCon            0L
  23. #define    kEmptyTitle            "\p"
  24.  
  25. #define    kEmptyString        "\p"
  26. #define kNilFilterProc        nil
  27.  
  28. #define    kErrorAlertID        kBaseResID
  29.  
  30.  
  31. /**************/
  32. /*  Globals      */
  33. /**************/
  34.  
  35. Boolean                gDone;
  36. ControlActionUPP    gActionUPP;
  37.  
  38.  
  39. /***************/
  40. /*  Functions  */
  41. /***************/
  42.  
  43. void    ToolBoxInit( void );
  44. void    WindowInit( void );
  45. void    SetUpScrollBar( WindowPtr window );
  46. pascal void ScrollProc( ControlHandle theControl, short partCode );
  47. void    EventLoop( void );
  48. void    DoEvent( EventRecord *eventPtr );
  49. void    HandleMouseDown( EventRecord *eventPtr );
  50. void    UpdateWindow( WindowPtr window );
  51. void    CenterPict( PicHandle picture, Rect *destRectPtr );
  52. void    DoError( Str255 errorString, Boolean fatal );
  53.  
  54.  
  55. /**************************** main **********************/
  56.  
  57. void    main( void )
  58. {
  59.     ToolBoxInit();
  60.     WindowInit();
  61.     
  62.     EventLoop();
  63. }
  64.  
  65.  
  66. /****************** ToolBoxInit *********************/
  67.  
  68. void    ToolBoxInit( void )
  69. {
  70.     InitGraf( &qd.thePort );
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs( 0L );
  76.     InitCursor();
  77. }
  78.  
  79.  
  80. /******************************** WindowInit *********/
  81.  
  82. void    WindowInit( void )
  83. {
  84.     WindowPtr    window;
  85.     
  86.     if ( ( window = GetNewWindow( kBaseResID, nil,
  87.                 kMoveToFront ) ) == nil )
  88.         DoError( "\pCan't Load WIND resource!", true );
  89.     
  90.     SetUpScrollBar( window );
  91.     
  92.     ShowWindow( window );
  93.     SetPort( window );
  94. }
  95.  
  96.  
  97. /**********************************    SetUpScrollBar    *******/
  98.  
  99. void    SetUpScrollBar( WindowPtr window )
  100. {
  101.     Rect            vScrollRect;
  102.     short            numPictures;
  103.     ControlHandle    scrollBarH;
  104.     
  105.     if ( ( numPictures = CountResources( 'PICT' ) ) <= 0 )
  106.         DoError( "\pNo PICT resources were found!", true );
  107.         
  108.     vScrollRect = window->portRect;
  109.     vScrollRect.top -= 1;
  110.     vScrollRect.bottom += 1;
  111.     vScrollRect.left = vScrollRect.right - kScrollBarWidth + 1;
  112.     vScrollRect.right += 1;
  113.     
  114.     scrollBarH = NewControl( window, &vScrollRect,
  115.             kEmptyTitle, kVisible, kStartValue, kMinValue,
  116.             numPictures, scrollBarProc, kNilRefCon );
  117. }
  118.  
  119.  
  120. /**********************************    ScrollProc    *******/
  121.  
  122. pascal void ScrollProc( ControlHandle theControl, short partCode )
  123. {
  124.     short        curCtlValue, maxCtlValue, minCtlValue;
  125.     WindowPtr    window;
  126.     
  127.     maxCtlValue = GetCtlMax( theControl );
  128.     curCtlValue = GetCtlValue( theControl );
  129.     minCtlValue = GetCtlMin( theControl );
  130.     
  131.     window = (**theControl).contrlOwner;
  132.     
  133.     switch ( partCode )
  134.     {
  135.         case inPageDown:
  136.         case inDownButton:
  137.             if ( curCtlValue < maxCtlValue )
  138.             {
  139.                 curCtlValue += 1;
  140.                 SetCtlValue( theControl, curCtlValue );
  141.                 UpdateWindow( window );
  142.             }
  143.             break;
  144.         case inPageUp:
  145.         case inUpButton:
  146.             if ( curCtlValue > minCtlValue )
  147.             {
  148.                 curCtlValue -= 1;
  149.                 SetCtlValue( theControl, curCtlValue );
  150.                 UpdateWindow( window );
  151.             }
  152.     }
  153. }
  154.  
  155.  
  156. /****************** EventLoop ***********************/
  157.  
  158. void    EventLoop( void )
  159. {        
  160.     EventRecord        event;
  161.     
  162.     gDone = false;
  163.     
  164.     while ( gDone == false )
  165.     {
  166.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  167.             DoEvent( &event );
  168.     }
  169. }
  170.  
  171.  
  172. /****************** DoEvent ***********************/
  173.  
  174. void    DoEvent( EventRecord *eventPtr )
  175. {
  176.     WindowPtr    window;
  177.     
  178.     switch ( eventPtr->what )
  179.     {
  180.         case mouseDown: 
  181.             HandleMouseDown( eventPtr );
  182.             break;
  183.         case updateEvt:
  184.             window = (WindowPtr)eventPtr->message;
  185.             
  186.             BeginUpdate( window );
  187.             DrawControls( window );
  188.             UpdateWindow( window );
  189.             EndUpdate( window );
  190.             break;
  191.     }
  192. }
  193.  
  194.  
  195. /****************** HandleMouseDown ***********************/
  196.  
  197. void    HandleMouseDown( EventRecord *eventPtr )
  198. {
  199.     WindowPtr            window;
  200.     short                thePart;
  201.     Point                thePoint;
  202.     ControlHandle        theControl;
  203.     
  204.     thePart = FindWindow( eventPtr->where, &window );
  205.     switch ( thePart )
  206.     {
  207.         case inSysWindow : 
  208.             SystemClick( eventPtr, window );
  209.             break;
  210.         case inDrag : 
  211.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  212.             break;
  213.         case inContent:
  214.             thePoint = eventPtr->where;
  215.             GlobalToLocal( &thePoint );
  216.             
  217.             thePart = FindControl( thePoint, window, &theControl );
  218.             
  219.             if ( theControl == ((WindowPeek)window)->controlList )
  220.             {
  221.                 if ( thePart == inThumb )
  222.                 {
  223.                     thePart = TrackControl( theControl, thePoint, kNilActionProc );
  224.                     InvalRect( &(window->portRect) );
  225.                 }
  226.                 else
  227.                 {
  228.                     gActionUPP = NewControlActionProc( ScrollProc );
  229.                     thePart = TrackControl( theControl, thePoint, gActionUPP );
  230.                 }
  231.             }
  232.             break;
  233.         case inGoAway : 
  234.             gDone = true;
  235.             break;
  236.     }
  237. }
  238.  
  239.  
  240. /**********************************    UpdateWindow    *******/
  241.  
  242. void    UpdateWindow( WindowPtr window )
  243. {
  244.     PicHandle    currentPicture;
  245.     Rect        windowRect;
  246.     RgnHandle    tempRgn;
  247.     
  248.     tempRgn = NewRgn();
  249.     GetClip( tempRgn );
  250.     
  251.     windowRect = window->portRect;
  252.     windowRect.right -= kScrollBarWidth;
  253.     EraseRect( &windowRect );
  254.     
  255.     ClipRect( &windowRect );
  256.     
  257.     currentPicture = (PicHandle)GetIndResource( 'PICT',
  258.             GetCtlValue( ((WindowPeek)window)->controlList ) );
  259.                         
  260.     if ( currentPicture == nil )
  261.         DoError( "\pCan't Load PICT resource!", true );
  262.         
  263.     CenterPict( currentPicture, &windowRect );
  264.     DrawPicture( currentPicture, &windowRect );
  265.     
  266.     SetClip( tempRgn );
  267.     DisposeRgn( tempRgn );
  268. }
  269.  
  270.  
  271. /****************** CenterPict ********************/
  272.  
  273. void    CenterPict( PicHandle picture, Rect *destRectPtr )
  274. {
  275.     Rect    windRect, pictRect;
  276.     
  277.     windRect = *destRectPtr;
  278.     pictRect = (**( picture )).picFrame;
  279.     OffsetRect( &pictRect, windRect.left - pictRect.left,
  280.                            windRect.top     - pictRect.top);
  281.     OffsetRect( &pictRect,(windRect.right - pictRect.right)/2,
  282.                           (windRect.bottom - pictRect.bottom)/2);
  283.     *destRectPtr = pictRect;
  284. }
  285.  
  286.  
  287. /***************** DoError ********************/
  288.  
  289. void    DoError( Str255 errorString, Boolean fatal )
  290. {
  291.     ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
  292.     
  293.     StopAlert( kErrorAlertID, kNilFilterProc );
  294.     
  295.     if ( fatal )
  296.         ExitToShell();
  297. }